if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}

#Load data and functions
source(paste0(base.path,"Drafts/Replication/03 Functions and Processing.R"))

###########################
## Figure 5 (Appendix B) ##
###########################

label_vec<-c(
  "educ_nodegreeTRUE"="Education (No degree)",
  "educ_baTRUE"="Education (BA)",
  "educ_advdegreeTRUE"="Education (Adv. Degree)",
  "maleTRUE"="Male",
  "hispanicbinTRUE"="Hispanic",
  "region1"="North",
  "region2"="Midwest",
  "region3"="South",
  "region4"="West",
  "whiteTRUE"="White",
  "blackTRUE"="Black",
  "hhi_num"="Household Income",
  "ideology7pt"="Ideology 7pt"
)

bh.obj.flda<-RItools::xBalance(
  fmla=as.formula(paste0("as.logical(as.numeric(treatment_automation_labor)-1)~",paste(balance_controls_vec,collapse=" + "))),
  #strata = list(party=~political_party_str,nationalism=~nationalism_questions_agreed),
  na.rm=TRUE,
  data=subset(lucid.proc, (treatment_fdal == "Domestic Automation" | treatment_fdal == "Foreign Labor")),
  report = c("std.diffs","p.values","chisquare.test"))

bh.obj.fada<-RItools::xBalance(
  fmla=as.formula(paste0("as.logical(as.numeric(treatment_foreign_domestic)-1)~",paste(balance_controls_vec,collapse=" + "))),
  #strata = list(party=~political_party_str,nationalism=~nationalism_questions_agreed),
  na.rm=TRUE,
  data=subset(lucid.proc, (treatment_fdal == "Domestic Automation" | treatment_fdal == "Foreign Automation")),
  report = c("std.diffs","p.values","chisquare.test"))

bh.flda.plt<-bh.obj.flda$results %>% 
  data.frame() %>% 
  tibble::rownames_to_column() %>% 
  rename(std.diff=std.diff.unstrat,p=p.unstrat) %>% 
  mutate(rowname=stringr::str_replace(rowname,"hispanic_strYes, another Hispanic, Latino, or Spanish origin \\*\\*\\* ","hispanic_str ")) %>%
  ggplot(aes(std.diff,rowname,color=interaction(p<0.05,p<0.1)))+
  geom_vline(xintercept=0,linetype="dashed",color="gray25")+
  geom_point()+
  scale_y_discrete(labels=label_vec)+
  scale_color_viridis_d(labels=c("p>0.1","p<0.1","p<0.05"),begin = 0.2,end=0.8)+
  coord_cartesian(xlim = c(-0.15,0.15))+
  theme_bw()+
  theme(legend.position="none",axis.text.y=element_text(size=5))+
  labs(x="Standardized Difference",
       y="Variable",
       color="Significance",
       title="Treatment (Foreign Lab. or Domestic Aut.)")

bh.fada.plt<-bh.obj.fada$results %>% 
  data.frame() %>% 
  tibble::rownames_to_column() %>% 
  rename(std.diff=std.diff.unstrat,p=p.unstrat) %>% 
  mutate(rowname=stringr::str_replace(rowname,"hispanic_strYes, another Hispanic, Latino, or Spanish origin \\*\\*\\* ","hispanic_str ")) %>%
  ggplot(aes(std.diff,rowname,color=interaction(p<0.05,p<0.1)))+
  geom_vline(xintercept=0,linetype="dashed",color="gray25")+
  geom_point()+
  scale_y_discrete(labels=label_vec)+
  scale_color_viridis_d(labels=c("p>0.10","p<0.1","p<0.05"),begin = 0.2,end=0.8)+
  coord_cartesian(xlim = c(-0.15,0.15))+
  theme_bw()+
  theme(legend.position="none",axis.text.y=element_text(size=5))+
  labs(x="Standardized Difference",
       y="Variable",
       color="Significance",
       title="Treatment (Foreign Aut. or Domestic Aut.)")

bh.legend<-cowplot::get_legend(bh.flda.plt+theme(legend.position="bottom"))

cowplot::plot_grid(cowplot::plot_grid(bh.flda.plt,bh.fada.plt,nrow=2),bh.legend,nrow=2,rel_heights = c(1,0.1))
